<?php

namespace Services\Api;

use Common\Models\Category;
use Common\Models\DouyinCategory;
use Common\Models\JdCategory;
use Services\ServicesApiBase;

class GoodsApiServices extends ServicesApiBase
{
    /**
     * @param $site 站点
     * @param int $pid 上级id
     * @return array
     */
    public function category($site, $pid = 0)
    {
        switch ($site) {
            case 'haodanku':
                $category = new Category();
                $res = $category->get_all($pid);
                break;
            case 'jd':
                $category = new JdCategory();
                $res = $category->category();
                break;
            case 'pdd':
                $res = $this->pdd->categroy();
                break;
            case 'vph':
                $res = $this->vph->get_categorys();
                break;
            case 'suning':
                $res = $this->suning->category_sort();
                break;
            case 'douyin':
                $douyinCategory = new DouyinCategory();
                $res = $douyinCategory->category();
                break;
        }
        $new_res[0] = [
            'category_id' => '-1',
            'site' => $site,
            'name' => '推荐',
            'image' => '',
        ];
        $res = array_merge($new_res, $res);
        return ['status' => true, 'msg' => '获取成功', 'data' => $res];
    }

    /**
     * @param $site
     */
    public function goods_list($site,$params = [])
    {
        switch ($site){
            case 'haodanku':
                $res = $this->zhetaoke->goods_list($params);
                break;
            case 'jd':
                $res = $this->jd->goods_list($params);
                break;
            case 'pdd':
                $res = $this->pdd->goods_list($params);
                break;
            case 'vph':
                $res = $this->vph->goods_list($params);
                break;
            case 'suning':
                $res = $this->suning->goods_list($params);
                break;
        }
        return $res;
    }


    /*
     * 商品详情
     */
    public function goods_detail($site,$goods_id,$pid=""){
        switch ($site){
            case 'haodanku':
                $res=$this->haodanku->goods_detail($goods_id);
                if(!$res['status']){
                    $res = $this->zhetaoke->goods_detail($goods_id);
                }
                break;
            case 'jd':
                $res=$this->jd->goods_detail($goods_id);
                break;
            case 'pdd':
                $res=$this->pdd->goods_detail($goods_id,$pid);
                break;
            case 'vph':
                $res=$this->vph->goods_detail($goods_id);
                break;
            case 'suning':
                $res = $this->suning->goods_detail($goods_id);
                break;
            case 'douyin':
                $res = $this->haodanku->douyin_goods_detail($goods_id);
                break;
        }
        if($res['status']){
            $res['data']['Title'] = "　  {$res['data']['Title']}";
        }

        return $res;
    }



    /**
     * 智能搜索
     */
    public function ai_search($user_info,$keyword){
        $returnLink = ['goodsId' => '', 'src' => 0, 'msg' => $keyword];
        $keyword = urldecode($keyword);
        $responseMsg = $this->zhetaoke->open_shangpin($keyword);
        if ($responseMsg['status']) {
            $returnLink['src'] = 1;
            $returnLink['goodsId'] = $responseMsg['goodsId'];
        } else {
            $flag = false;      //标记 记录是否解析出了

            //解析大淘客
            $responseMsg = $this->taoke->contentParse($keyword);
            if($responseMsg['status']){
                $returnLink['src'] = $responseMsg['src'];
                $returnLink['goodsId'] = $responseMsg['goodsId'];
                $flag = true;
            }


            if ((strpos($keyword, 'taobao.com') !== false || strpos($keyword, 'tb.cn') !== false) && !$flag) {    //淘宝链接和短连接
                $responseMsg = $this->taoke->parsecontent($keyword);
                if ($responseMsg['status']) {
                    $returnLink['src'] = 1;
                    $returnLink['goodsId'] = $responseMsg['goodsId'];
                    $flag = true;
                }
            }

            preg_match('/(http|https|ftp)(.)*([a-z0-9\-\.\_])+/i', $keyword, $array2);
            if ((isset($array2[0]) && !empty($array2[0]) && (strpos($keyword, 'p.pinduoduo.com') !== false)) && !$flag) {//如果是拼多多的短连接
                $responseMsg = $this->pdd->haojingkeGoods($array2[0]);
                if (!$responseMsg['status']) {
                    $responseMsg = $this->pdd->haojingkeDetails($array2[0]);
                }
                if ($responseMsg['status']) {
                    $returnLink['src'] = 3;
                    $returnLink['goodsId'] = $responseMsg['goodsId'];
                    $flag = true;
                }
            }

            if (strpos($keyword, 'yangkeduo') !== false && !$flag) {   //匹配pdd的链接地址
                $keywordQuery = parse_url($keyword);
                if ($keywordQuery && isset($keywordQuery['query'])) {
                    parse_str($keywordQuery['query'], $keywordQueryParams);
                    if (isset($keywordQueryParams['goods_id'])) {
                        $responseMsg = $this->pdd->haojingkeGoods($keywordQueryParams['goods_id']);
                        if (!$responseMsg['status']) {
                            $responseMsg = $this->pdd->haojingkeDetails($keywordQueryParams['goods_id']);
                        }
                        if ($responseMsg['status']) {
                            $returnLink['src'] = 3;
                            $returnLink['goodsId'] = $responseMsg['goodsId'];
                            $flag = true;
                        }
                    }
                }
            }

            if (strpos($keyword, 'vip.com') !== false && !$flag) {               //唯品会
                $viparr = explode("-", $keyword);
                if (isset($viparr[2])) {
                    $returnLink['src'] = 4;
                    $returnLink['goodsId'] = substr($viparr[2], 0, strpos($viparr[2], '.'));
                    $flag = true;
                }
            }

            if (isset($array2[0]) && !empty($array2[0]) && (strpos($keyword, 'u.jd.com') !== false) && !$flag) {//京东短连接
                $responseMsg = $this->jd->short_url_to_goodsid($array2[0]);
                if ($responseMsg['status']) {
                    $returnLink['src'] = 2;
                    $returnLink['goodsId'] = $responseMsg['goodsId'];
                    $flag = true;
                }
            }

            if (strpos($keyword, 'm.jd.com') !== false && !$flag) {               //京东长连接
                $responseMsg = $this->jd->short_url_to_goodsid($keyword);
                if ($responseMsg['status']) {
                    $returnLink['src'] = 2;
                    $returnLink['goodsId'] = $responseMsg['goodsId'];
                    $flag = true;
                }
            }

        }


        $responseResult = ['status' => false, 'data' => null, 'msg' => $keyword];
        switch ($returnLink['src']) {
            case 1:
                $detailResult = $this->zhetaoke->goods_detail($returnLink['goodsId']);
                if(!$detailResult['status']){
                    $detailResult = $this->haodanku->goods_detail($returnLink['goodsId']);
                }
                break;
            case 2:
                $detailResult = $this->jd->goods_detail($returnLink['goodsId']);
                break;
            case 3:
                $detailResult = $this->pdd->goods_detail($returnLink['goodsId']);
                break;
            case 4:
                $detailResult = $this->vph->goods_detail($returnLink['goodsId']);
                break;
            case 5:
                $detailResult = $this->suning->goods_detail($returnLink['goodsId']);
                break;
            default:
                return $responseResult;
                break;
        }
        if($detailResult['status']){
            return $detailResult;
        }else{
            return $responseResult;
        }

    }


    //获取抖音首页分类数据
    public function dhindex($params){
        $dyCategoryList = $this->haodanku->dyCategoryList($params);
        $responseResult = ['status'=>false,'msg'=>'获取失败','data'=>[]];
        if($dyCategoryList['status']){
            $tempResponse['category'] = [
                ['categoryId'=>1,'name'=>'实时热榜'],
                ['categoryId'=>2,'name'=>'30日销量榜'],
                ['categoryId'=>3,'name'=>'7日销量榜'],
            ];
            $tempResponse['child'] = $dyCategoryList['data'];
            $responseResult['data'] = $tempResponse;
            $responseResult['status'] = true;
        }else{
            $dyCategoryList['msg'] = $dyCategoryList['msg'];
        }
        return $responseResult;
    }


    /**
     * 抖音榜单
     */
    public function dyRankitemList($params){
        $dyCategoryList = $this->haodanku->dyRankitemList($params);
        return $dyCategoryList;
    }
}